
import {
  response,
  errorHandle,
  url
} from './response';

const getToken = () => {
  return wx.getStorageSync('auth');
}
/**
 * 查询原料成分数据列表
 * @param {*} data 
 * @param {*} successCallback 
 */
export const list = (data, successCallback, reject) => {
    wx.request({
      url: `${url}/ingredients/list`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback, reject);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
        reject && reject();
      }
    })
  }
  /**
   * 搜索原料成分数据列表
   * @param {*} data 
   * @param {*} successCallback 
   */
  export const search = (data, successCallback) => {
    wx.request({
      url: `${url}/ingredients/search`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  }
  /**
   * 删除原料成分数据列表
   * @param {*} data 
   * @param {*} successCallback 
   */
 export const remove = (data, successCallback)=> {
    wx.request({
      url: `${url}/ingredients/remove`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  }
  export const add =  (data, successCallback)=> {
   wx.request({
     url: `${url}/ingredients/add`,
     method: 'post',
     data,
     header: {
       'x-user-agent': 'miniprogram',
       'x-auth-token': getToken()
     },
     success(res) {
       response(res, successCallback);
     },
     fail: (res) => {
       errorHandle(res.errMsg);
     }
   })
 }
 export const _get = (id, successCallback) =>{
  wx.request({
    url: `${url}/ingredients/${id}`,
    method: 'get',
    header: {
      'x-user-agent': 'miniprogram',
      'x-auth-token': getToken()
    },
    success(res) {
      response(res, successCallback);
    },
    fail: (res) => {
      errorHandle(res.errMsg);
    }
  })
}

export const update = (data, successCallback)=> {
  wx.request({
    url: `${url}/ingredients/update`,
    method: 'post',
    data,
    header: {
      'x-user-agent': 'miniprogram',
      'x-auth-token': getToken()
    },
    success(res) {
      response(res, successCallback);
    },
    fail: (res) => {
      errorHandle(res.errMsg);
    }
  })
}
export const statusUpdate = (data, successCallback)=> {
  wx.request({
    url: `${url}/ingredients/update/status`,
    method: 'post',
    data,
    header: {
      'x-user-agent': 'miniprogram',
      'x-auth-token': getToken()
    },
    success(res) {
      response(res, successCallback);
    },
    fail: (res) => {
      errorHandle(res.errMsg);
    }
  })
}